package com.skyable.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author GaoYuan
 * @date 2019/11/4 15:12
 * @desc 各类时间类型转换
 * --Java的ISO、UTC、UNIX时间类型转换
 */
public class UTCTransferUtil {

    private static SimpleDateFormat format             = new SimpleDateFormat(
        "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

    private static SimpleDateFormat utc_iso8601_Format = new SimpleDateFormat(
        "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

    private static SimpleDateFormat utc_Format         = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");

    private static SimpleDateFormat bjFormat           = new SimpleDateFormat(
        "yyyy-MM-dd HH:mm:ss");

    static {
        //设置utc时区
        utc_iso8601_Format.setTimeZone(TimeZone.getTimeZone("UTC"));
        utc_Format.setTimeZone(TimeZone.getTimeZone("UTC"));
        //设置北京时区
        bjFormat.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

    }

    /**
     * 将UTC时间转化为北京时间 
     * --（将ISO8601格式的UTC时间，转化为普通格式的北京时间）
     * @param utcTime "2017-11-27T03:16:03.944Z"  （ISO8601格式）
     * @return
     */
    private synchronized static String utciso86012Beijingtime(String utcTime) {
        if (utcTime == null || "".equals(utcTime)) {
            return "";
        }
        Date gpsUTCDate = null;
        try {
            gpsUTCDate = utc_iso8601_Format.parse(utcTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String localTime = bjFormat.format(gpsUTCDate.getTime());
        return localTime;
    }

    /**
     * 将UTC时间转化为北京时间 
     * --(将普通格式的时间+8个小时)
     * @param utcTime "2018-09-17 07:36:00"  （"yyyy-MM-dd HH:mm:ss"格式）
     * @return   2018-09-17 15:36:00
     */
    private synchronized static String utc2BeiJingTime(String utcTime) {

        if (utcTime == null || "".equals(utcTime)) {
            return utcTime;
        }
        Date gpsUTCDate = null;
        try {
            gpsUTCDate = utc_Format.parse(utcTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String localTime = bjFormat.format(gpsUTCDate.getTime());
        return localTime;
    }

    /**
     * 将UTC时间转化为北京时间 
     * --(将普通格式的时间+8个小时)
     * @param utcTime   utc时间
     * @return
     */
    private synchronized static Date utc2BeiJingTime(Date utcTime) {

        String localTime = bjFormat.format(utcTime.getTime());

        Date localDate = null;
        try {
            localDate = bjFormat.parse(localTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return localDate;
    }

    /**
     * UTC时间转换为本地时间
     * @param utcTime "2017-11-27T03:16:03.944Z"
     * @param utcTimePatten "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
     * @param localTimePatten "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    private static String utc2Local(String utcTime, String utcTimePatten, String localTimePatten) {
        SimpleDateFormat utcFormater = new SimpleDateFormat(utcTimePatten);
        utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
        Date gpsUTCDate = null;
        try {
            gpsUTCDate = utcFormater.parse(utcTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        SimpleDateFormat localFormater = new SimpleDateFormat(localTimePatten);
        localFormater.setTimeZone(TimeZone.getDefault());
        String localTime = localFormater.format(gpsUTCDate.getTime());
        return localTime;
    }

    /**
     * 
     * @param UTCStr  "2017-11-27T03:16:03.944Z"
     * @param format  "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
     * @throws ParseException
     */
    private static void UTCToCST(String UTCStr, String format) throws ParseException {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        date = sdf.parse(UTCStr);
        System.out.println("UTC时间: " + date);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR, calendar.get(Calendar.HOUR) + 8);
        //calendar.getTime() 返回的是Date类型，也可以使用calendar.getTimeInMillis()获取时间戳
        System.out.println("北京时间: " + calendar.getTime());
    }

    private synchronized static String unixToIso(long unix) {

        Date time = new Date(unix);

        return format.format(time);

    }

    private static long unixAdd8(long unix) {

        unix = unix + 1000 * 60 * 60 * 8L;

        return unix;

    }

    private static Date isoToUtc(String iso) throws ParseException {

        return format.parse(iso);

    }

    private static long isoToUnix(String iso) throws ParseException {

        Date time = format.parse(iso);

        long uinx = time.getTime();

        return uinx;

    }

    public static void main(String[] args) throws ParseException {

        String local1 = "2018-09-17 07:36:00";
        Date gpsUTCDate = null;
        try {
            gpsUTCDate = utc_Format.parse(local1);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println(utc2BeiJingTime(gpsUTCDate));

        System.out.println(utc2BeiJingTime("2018-09-17 23:36:00"));
        System.out.println(utciso86012Beijingtime("2017-11-27T03:16:03.944Z"));
        System.out.println(UTCTransferUtil.utciso86012Beijingtime("2018-08-13T02:40:19.906Z"));
    }

}
